<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <style type="text/css">
        .aui-chat .aui-chat-header {
            margin-bottom: 0px;
        }

        .aui-chat .aui-chat-content_img {
            color: #212121;
            font-size: 0.7rem;
            border-radius: 0.2rem;
            min-height: 2rem;
            min-width: 4rem;
            position: relative;
            padding: 0.3rem;
            max-width: 44%;
            word-break: break-all;
            word-wrap: break-word;
        }

        .aui-chat .aui-chat-content_img img {
            max-width: 100%;
            display: inline;
            border-radius: 0.2rem;
        }

        .aui-chat .aui-chat-left .aui-chat-content_img {
            background-color: #b3e5fc;
            float: left;
            left: 0.5rem;
        }

        .aui-chat .aui-chat-right .aui-chat-content_img {
            background-color: #ffffff;
            right: 0.5rem;
            float: right;
        }

        .voice {
            height: 1.3rem;
            line-height: 1.3rem;
            width: 4rem;
            position: relative;
            background-image: url(../res/ChatBox/stop.png);
            background-repeat: no-repeat;
            background-position: 0 -0.1rem;
            background-size: 1.4rem auto;
            padding-left: 1.4rem;
            color: #424242;
        }

        #msglist {

        }
    </style>
</head>
<body>
<section class="aui-chat" id="msglist"></section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript">
    apiready = function () {
        var winheight = api.winHeight;
        var rong = api.require('rongCloud2');
        var UIChatBox = api.require('UIChatBox');
        getLatestMessages(api.pageParam.id, 'PRIVATE');
        UIChatBox.open({
            placeholder: '',
            maxRows: 4,
            emotionPath: 'widget://res/ChatBox/emotion',
            texts: {
                recordBtn: {
                    normalTitle: '按住  说话',
                    activeTitle: '松开  结束'
                },
                sendBtn: {
                    title: '发送'
                }
            },
            styles: {
                inputBar: {
                    borderColor: '#ececec',
                    bgColor: '#fbfbfb'
                },
                inputBox: {
                    borderColor: '#B3B3B3',
                    bgColor: '#FFFFFF'
                },
                emotionBtn: {
                    normalImg: 'widget://res/ChatBox/face1.png'
                },
                extrasBtn: {
                    normalImg: 'widget://res/ChatBox/add1.png'
                },
                keyboardBtn: {
                    normalImg: 'widget://res/ChatBox/key1.png'
                },
                // speechBtn: {
                //     normalImg: 'widget://res/ChatBox/rec.png'
                // },
                recordBtn: {
                    normalBg: '#50b6dc',
                    activeBg: '#3FBC7D',
                    color: '#FFF',
                    size: 16
                },
                indicator: {
                    target: 'both',
                    color: '#c4c4c4',
                    activeColor: '#9e9e9e'
                },
                sendBtn: {
                    titleColor: '#ffffff',
                    bg: '#12b7f5',
                    activeBg: '#1ba1d4',
                    titleSize: 14
                }
            },
            // extras: {
            //     titleSize: 16,
            //     titleColor: '#a3a3a3',
            //     btns: [{
            //         title: '相册图片',
            //         normalImg: 'widget://res/ChatBox/img1.png',
            //         activeImg: 'widget://res/ChatBox/img2.png'
            //     }, {
            //         title: '相机拍照',
            //         normalImg: 'widget://res/ChatBox/cam1.png',
            //         activeImg: 'widget://res/ChatBox/cam2.png'
            //     }]
            // }
        }, function (ret, err) {
            var eventType = ret.eventType;
            var msg = ret.msg;
            if (eventType == 'clickExtras') {
                if (ret.index == 0) {
                    getPicture('library')
                    pageDown(300);
                } else if (ret.index == 1) {
                    getPicture('camera')
                    pageDown(300);
                }
            }
            if (eventType == 'send') {
                if ($api.trimAll(ret.msg) == '') {
                    api.toast({
                        msg: '不能发送空白消息',
                        duration: 1000,
                        location: 'center'
                    });
                } else {
                    var extra = {
                        xm1: $api.getStorage('zh'),
                        xm2: api.pageParam.zh,
                        tx1: $api.getStorage('tx'),
                        tx2: api.pageParam.tx
                    };
                    rong.sendTextMessage({
                        conversationType: 'PRIVATE',
                        targetId: api.pageParam.id,
                        text: msg,
                        extra: JSON.stringify(extra)
                    }, function (ret, err) {
                        console.log(JSON.stringify(ret))
                        if (ret.status == 'prepare') {
                            xx = ret.result.message.content.text.replace(reg, function (a, b) {
                                return face[a] ? face[a] : a;
                            });
                        } else if (ret.status == 'success') {
                            var html = '';
                            html += '<div class="aui-chat-item aui-chat-right">';
                            html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                            html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                            html += '	       <div class="aui-chat-content">';
                            html += '		        <div class="aui-chat-arrow"></div>';
                            html += xx;
                            html += '               </div>';
                            html += '	  </div>';
                            html += '</div>';
                            $api.append($api.dom('#msglist'), html);
                            pageDown(300);
                        }
                    });
                }
            }
        });
        UIChatBox.addEventListener({
            target: 'inputBar',
            name: 'move'
        }, function (ret, err) {
            console.log(JSON.stringify(ret));
            if (ret) {
                pageDown(300);
                var h1 = winheight - 70 - ret.inputBarHeight - ret.panelHeight;
                console.log(h1);
                api.setFrameAttr({
                    name: 'frame_chat',
                    rect: {
                        x: 0,
                        y: 70,
                        w: 'auto',
                        h: h1
                    }
                });
                pageDown(300);
            }
        });
        UIChatBox.addEventListener({
            target: 'recordBtn',
            name: 'press'
        }, function (ret, err) {
            sj1 = new Date().getTime();
            if (ret) {
                var savePath = 'fs://lysd_' + (+new Date()) + '.amr';
                api.startRecord({
                    path: savePath
                });
            }
        });
        UIChatBox.addEventListener({
            target: 'recordBtn',
            name: 'press_cancel'
        }, function (ret, err) {
            var sj2 = new Date().getTime() - sj1;
            if (sj2 <= 2000) {
                stopRecord();
                api.toast({
                    msg: '语音时间较短，请重新录音',
                    duration: 3000,
                    location: 'middle'
                });
            } else if (sj2 > 2000) {
                api.stopRecord(function (ret, err) {
                    if (ret.duration > 0) {
                        var path = ret.path;
                        var duration = ret.duration;
                        var extra = {
                            xm1: $api.getStorage('zh'),
                            xm2: api.pageParam.zh,
                            tx1: $api.getStorage('tx'),
                            tx2: api.pageParam.tx
                        };
                        rong.sendVoiceMessage({
                            conversationType: 'PRIVATE',
                            targetId: api.pageParam.id,
                            voicePath: path,
                            duration: duration,
                            extra: JSON.stringify(extra)
                        }, function (ret, err) {
                            if (ret.status == 'prepare') {
                                yy = ret.result.message.content.voicePath;
                                sj_len = ret.result.message.content.duration;
                            } else if (ret.status == 'success') {
                                var html = '';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + yy + '\')" class="voice"> ' + sj_len + ' "</div>';
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                                $api.append($api.dom('#msglist'), html);
                                pageDown(200);
                                api.sendEvent({
                                    name: 'senthh'
                                });
                            } else if (ret.status == 'error') {
                                api.toast({
                                    msg: '语音发送失败'
                                });
                            }
                        });
                    }
                });
            }
        });
        UIChatBox.addEventListener({
            target: 'recordBtn',
            name: 'move_in'
        }, function (ret, err) {
            sj1 = new Date().getTime();
            if (ret) {
                var savePath = 'fs://lysd_' + (+new Date()) + '.amr';
                api.startRecord({
                    path: savePath
                });
            }
        });
        UIChatBox.addEventListener({
            target: 'recordBtn',
            name: 'move_out_cancel'
        }, function (ret, err) {
            var sj2 = new Date().getTime() - sj1;
            if (sj2 <= 2000) {
                stopRecord();
                api.toast({
                    msg: '语音时间较短，请重新录音',
                    duration: 3000,
                    location: 'middle'
                });
            } else if (sj2 > 2000) {
                api.stopRecord(function (ret, err) {
                    if (ret.duration > 0) {
                        var path = ret.path;
                        var duration = ret.duration;
                        var extra = {
                            xm1: $api.getStorage('zh'),
                            xm2: api.pageParam.zh,
                            tx1: $api.getStorage('tx'),
                            tx2: api.pageParam.tx
                        };
                        rong.sendVoiceMessage({
                            conversationType: 'PRIVATE',
                            targetId: api.pageParam.id,
                            voicePath: path,
                            duration: duration,
                            extra: JSON.stringify(extra)
                        }, function (ret, err) {
                            if (ret.status == 'prepare') {
                                yy = ret.result.message.content.voicePath;
                                sj_len = ret.result.message.content.duration;
                            } else if (ret.status == 'success') {
                                var html = '';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + yy + '\')" class="voice"> ' + sj_len + ' "</div>';
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                                $api.append($api.dom('#msglist'), html);
                                pageDown(200);
                                api.sendEvent({
                                    name: 'senthh'
                                });
                            } else if (ret.status == 'error') {
                                api.toast({
                                    msg: '语音发送失败'
                                });
                            }
                        });
                    }
                });
            }
        });
        api.addEventListener({
            name: 'new_msg'
        }, function (ret, err) {
            var html = '';
            if (ret.value.msg.targetId == api.pageParam.id) {
                api.notification({
                    sound: 'default',
                    vibrate:[100, 500, 200, 500]
                });
                if (ret.value.msg.objectName == 'RC:TxtMsg') {
                    txt = ret.value.msg.content.text.replace(reg, function (a, b) {
                        return face[a] ? face[a] : a;
                    });
                    html += '<div class="aui-chat-header">' + sj(ret.value.msg.sentTime) + '</div>';
                    html += '<div class="aui-chat-item aui-chat-left">';
                    html += '	  <div class="aui-chat-media"><img src="' + api.pageParam.tx + '" /></div>';
                    html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + api.pageParam.zh + '</div>';
                    html += '	       <div class="aui-chat-content">';
                    html += '		        <div class="aui-chat-arrow"></div>';
                    html += txt;
                    html += '               </div>';
                    html += '	  </div>';
                    html += '</div>';
                } else if (ret.value.msg.objectName == 'RC:ImgMsg') {
                    html += '<div class="aui-chat-header">' + sj(ret.value.msg.sentTime) + '</div>';
                    html += '<div class="aui-chat-item aui-chat-left">';
                    html += '	  <div class="aui-chat-media"><img src="' + api.pageParam.tx + '" /></div>';
                    html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + api.pageParam.zh + '</div>';
                    html += '	       <div class="aui-chat-content_img">';
                    html += '		        <div class="aui-chat-arrow"></div>';
                    html += '               <img onclick="openImage(\'' + ret.value.msg.content.imageUrl + '\')" src="' + ret.value.msg.content.thumbPath + '"/>';
                    html += '          </div>';
                    html += '	  </div>';
                    html += '</div>';
                } else if (ret.value.msg.objectName == 'RC:VcMsg') {
                    html += '<div class="aui-chat-header">' + sj(ret.value.msg.sentTime) + '</div>';
                    html += '<div class="aui-chat-item aui-chat-left">';
                    html += '	  <div class="aui-chat-media"><img src="' + api.pageParam.tx + '" /></div>';
                    html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + api.pageParam.zh + '</div>';
                    html += '	       <div class="aui-chat-content_img">';
                    html += '		        <div class="aui-chat-arrow"></div>';
                    html += '<div onclick="play(this,\'' + ret.value.msg.content.voicePath + '\')" class="voice"> ' + ret.value.msg.content.duration + ' "</div>';
                    html += '          </div>';
                    html += '	  </div>';
                    html += '</div>';
                }
                $api.append($api.dom('#msglist'), html);
                pageDown(300);
            }
        });
        api.setCustomRefreshHeaderInfo({
            bgColor: '#ffffff',
            image: {
                pull: ['widget://image/1.png', 'widget://image/2.png', 'widget://image/3.png', 'widget://image/4.png', 'widget://image/5.png', 'widget://image/6.png', 'widget://image/7.png', 'widget://image/8.png'],
                load: ['widget://image/8.png', 'widget://image/7.png', 'widget://image/6.png', 'widget://image/5.png', 'widget://image/4.png', 'widget://image/3.png', 'widget://image/2.png', 'widget://image/1.png']
            }
        }, function () {
            getHistoryMessages(api.pageParam.id, 'PRIVATE');
            setTimeout(function () {
                api.refreshHeaderLoadDone();
            }, 3000)
        });
    }

    function getPicture(type) {
        api.getPicture({
            sourceType: type,
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: false,
            quality: 100,
            saveToPhotoAlbum: false
        }, function (ret, err) {
            if (ret) {
                var extra = {
                    xm1: $api.getStorage('zh'),
                    xm2: api.pageParam.zh,
                    tx1: $api.getStorage('tx'),
                    tx2: api.pageParam.tx
                };
                var rong = api.require('rongCloud2');
                rong.sendImageMessage({
                    conversationType: 'PRIVATE',
                    targetId: api.pageParam.id,
                    imagePath: ret.data,
                    extra: JSON.stringify(extra)
                }, function (ret, err) {
                    if (ret.status == 'prepare') {
                        var bpic = ret.result.message.content.imageUrl;
                        var spic = ret.result.message.content.thumbPath;
                        var html = '';
                        html += '<div class="aui-chat-item aui-chat-right">';
                        html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                        html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                        html += '	       <div class="aui-chat-content_img">';
                        html += '		        <div class="aui-chat-arrow"></div>';
                        html += '               <img onclick="openImage(\'' + bpic + '\')" src="' + spic + '"/>';
                        html += '          </div>';
                        html += '	  </div>';
                        html += '</div>';
                        $api.append($api.dom('#msglist'), html);
                    }
                });
                var UIChatBox = api.require('UIChatBox');
                UIChatBox.closeBoard();
            } else {
                api.toast({
                    msg: '图像获取失败',
                    duration: 3000,
                    location: 'bottom'
                });
                var UIChatBox = api.require('UIChatBox');
                UIChatBox.closeBoard();
            }
        });
    }

    //获取最后一段会话
    function getLatestMessages(targetId, conversationType) {
        var rong = api.require('rongCloud2');
        rong.getLatestMessages({
            conversationType: conversationType,
            targetId: targetId,
            count: 4
        }, function (ret, err) {
            if (ret.status == 'success') {
                if (ret.result && ret.result.length > 0) {
                    var html = '';
                    for (var i = ret.result.length - 1; i >= 0; i--) {
                        var rs = ret.result[i];
                        var extra = transExtra(rs.content.extra);
                        if (extra.xm1 == $api.getStorage('zh')) {
                            var xm = extra.xm2;
                            var tx = extra.tx2;
                        } else {
                            var xm = extra.xm1;
                            var tx = extra.tx1;
                        }
                        if (rs.objectName == 'RC:TxtMsg') {
                            jsxx = rs.content.text.replace(reg, function (a, b) {
                                return face[a] ? face[a] : a;
                            });
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += jsxx;
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += jsxx;
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        } else if (rs.objectName == 'RC:ImgMsg') {
                            var bpic = rs.content.imageUrl;
                            var spic = rs.content.thumbPath;
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '               <img onclick="openImage(\'' + bpic + '\')" src="' + spic + '"/>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '               <img onclick="openImage(\'' + bpic + '\')" src="' + spic + '"/>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        } else if (rs.objectName == 'RC:VcMsg') {
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + rs.content.voicePath + '\')" class="voice"> ' + rs.content.duration + ' "</div>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + rs.content.voicePath + '\')" class="voice"> ' + rs.content.duration + ' "</div>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        }
                    }
                    $api.html($api.dom('#msglist'), html);
                    pageDown(300);
                }
            }
        })
    }

    //下拉加载历史记录
    function getHistoryMessages(targetId, conversationType) {
        var rong = api.require('rongCloud2');
        var oldid = $(".aui-chat-header").first().data("id");
        rong.getHistoryMessages({
            conversationType: conversationType,
            targetId: targetId,
            oldestMessageId: oldid,
            count: 8
        }, function (ret, err) {
            if (ret.status == 'success') {
                var html = '';
                if (ret.result && ret.result.length > 0) {
                    for (var i = ret.result.length - 1; i >= 0; i--) {
                        var rs = ret.result[i];
                        var extra = transExtra(rs.content.extra);
                        if (extra.xm1 == $api.getStorage('zh')) {
                            var xm = extra.xm2;
                            var tx = extra.tx2;
                        } else {
                            var xm = extra.xm1;
                            var tx = extra.tx1;
                        }
                        if (rs.objectName == 'RC:TxtMsg') {
                            jsxx = rs.content.text.replace(reg, function (a, b) {
                                return face[a] ? face[a] : a;
                            });
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media"><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += jsxx;
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += jsxx;
                                html += '               </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        } else if (rs.objectName == 'RC:ImgMsg') {
                            var bpic = rs.content.imageUrl;
                            var spic = rs.content.thumbPath;
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media" ><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '               <img onclick="openImage(\'' + bpic + '\')" src="' + spic + '"/>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '               <img onclick="openImage(\'' + bpic + '\')" src="' + spic + '"/>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        } else if (rs.objectName == 'RC:VcMsg') {
                            if (rs.messageDirection == 'SEND') {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-right">';
                                html += '	  <div class="aui-chat-media" ><img src="' + $api.getStorage('tx') + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + $api.getStorage('zh') + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + rs.content.voicePath + '\')" class="voice"> ' + rs.content.duration + ' "</div>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            } else {
                                html += '<div class="aui-chat-header" data-id="' + rs.messageId + '">' + sj(rs.sentTime) + '</div>';
                                html += '<div class="aui-chat-item aui-chat-left">';
                                html += '	  <div class="aui-chat-media"><img src="' + tx + '" /></div>';
                                html += '	  <div class="aui-chat-inner"><div class="aui-chat-name">' + xm + '</div>';
                                html += '	       <div class="aui-chat-content_img">';
                                html += '		        <div class="aui-chat-arrow"></div>';
                                html += '<div onclick="play(this,\'' + rs.content.voicePath + '\')" class="voice"> ' + rs.content.duration + ' "</div>';
                                html += '          </div>';
                                html += '	  </div>';
                                html += '</div>';
                            }
                        }
                    }
                    $api.prepend($api.dom('#msglist'), html);
                    api.refreshHeaderLoadDone();
                } else {
                    api.toast({
                        msg: '木有了消息啦…'
                    });
                    api.refreshHeaderLoadDone();
                }
            }
        })
    }

    //滑动到底部
    function pageDown(time) {
        setTimeout(function () {
            api.pageDown({
                bottom: true,
                animate: true
            }, function (ret) {
            });
        }, time || 0)
    }

    //终止录音
    function stopRecord() {
        api.stopRecord(function (ret, err) {
        });
    }

    //播放录音
    function play(el, path) {
        $api.css(el, 'background-image:url(../res/ChatBox/playing.gif)');
        api.startPlay({
            path: path
        }, function () {
            $api.css(el, 'background-image:url(../res/ChatBox/stop.png)');
        });
    }

    //表情符号转换
    var reg = /\[.+?\]/g;
    var face = {
        '[微笑]': '<span><img src="../res/ChatBox/emotion/Expression_1.png"  width="28"/></span>',
        '[撇嘴]': '<span><img src="../res/ChatBox/emotion/Expression_2.png"  width="28" /></span>',
        '[色]': '<span><img src="../res/ChatBox/emotion/Expression_3.png"  width="28" /></span>',
        '[发呆]': '<span><img src="../res/ChatBox/emotion/Expression_4.png"  width="28" /></span>',
        '[得意]': '<span><img src="../res/ChatBox/emotion/Expression_5.png"  width="28" /></span>',
        '[流泪]': '<span><img src="../res/ChatBox/emotion/Expression_6.png"  width="28" /></span>',
        '[害羞]': '<span><img src="../res/ChatBox/emotion/Expression_7.png"  width="28" /></span>',
        '[闭嘴]': '<span><img src="../res/ChatBox/emotion/Expression_8.png"  width="28" /></span>',
        '[睡]': '<span><img src="../res/ChatBox/emotion/Expression_9.png"  width="28" /></span>',
        '[大哭]': '<span><img src="../res/ChatBox/emotion/Expression_10.png"  width="28"/></span>',
        '[尴尬]': '<span><img src="../res/ChatBox/emotion/Expression_11.png"  width="28"/></span>',
        '[发怒]': '<span><img src="../res/ChatBox/emotion/Expression_12.png"  width="28"/></span>',
        '[调皮]': '<span><img src="../res/ChatBox/emotion/Expression_13.png"  width="28" /></span>',
        '[呲牙]': '<span><img src="../res/ChatBox/emotion/Expression_14.png"  width="28" /></span>',
        '[惊讶]': '<span><img src="../res/ChatBox/emotion/Expression_15.png"  width="28" /></span>',
        '[难过]': '<span><img src="../res/ChatBox/emotion/Expression_16.png"  width="28" /></span>',
        '[酷]': '<span><img src="../res/ChatBox/emotion/Expression_17.png"  width="28" /></span>',
        '[冷汗]': '<span><img src="../res/ChatBox/emotion/Expression_18.png"  width="28" /></span>',
        '[抓狂]': '<span><img src="../res/ChatBox/emotion/Expression_19.png"  width="28" /></span>',
        '[吐]': '<span><img src="../res/ChatBox/emotion/Expression_20.png"  width="28" /></span>',
        '[偷笑]': '<span><img src="../res/ChatBox/emotion/Expression_21.png"  width="28" /></span>',
        '[愉快]': '<span><img src="../res/ChatBox/emotion/Expression_22.png"  width="28" /></span>',
        '[白眼]': '<span><img src="../res/ChatBox/emotion/Expression_23.png"  width="28" /></span>',
        '[傲慢]': '<span><img src="../res/ChatBox/emotion/Expression_24.png"  width="28" /></span>',
        '[饥饿]': '<span><img src="../res/ChatBox/emotion/Expression_25.png"  width="28" /></span>',
        '[困]': '<span><img src="../res/ChatBox/emotion/Expression_26.png"  width="28" /></span>',
        '[恐惧]': '<span><img src="../res/ChatBox/emotion/Expression_27.png"  width="28" /></span>',
        '[流汗]': '<span><img src="../res/ChatBox/emotion/Expression_28.png"  width="28" /></span>',
        '[憨笑]': '<span><img src="../res/ChatBox/emotion/Expression_29.png"  width="28" /></span>',
        /*从这*/
        '[悠闲]': '<span><img src="../res/ChatBox/emotion/Expression_30.png"  width="28" /></span>',
        '[奋斗]': '<span><img src="../res/ChatBox/emotion/Expression_31.png"  width="28" /></span>',
        '[咒骂]': '<span><img src="../res/ChatBox/emotion/Expression_32.png"  width="28" /></span>',
        '[疑问]': '<span><img src="../res/ChatBox/emotion/Expression_33.png"  width="28" /></span>',
        '[嘘]': '<span><img src="../res/ChatBox/emotion/Expression_34.png"  width="28" /></span>',
        '[晕]': '<span><img src="../res/ChatBox/emotion/Expression_35.png"  width="28" /></span>',
        '[疯了]': '<span><img src="../res/ChatBox/emotion/Expression_36.png"  width="28" /></span>',
        '[衰]': '<span><img src="../res/ChatBox/emotion/Expression_37.png"  width="28" /></span>',
        '[骷髅]': '<span><img src="../res/ChatBox/emotion/Expression_38.png"  width="28" /></span>',
        '[敲打]': '<span><img src="../res/ChatBox/emotion/Expression_39.png"  width="28"/></span>',
        '[再见]': '<span><img src="../res/ChatBox/emotion/Expression_40.png"  width="28"/></span>',
        '[擦汗]': '<span><img src="../res/ChatBox/emotion/Expression_41.png"  width="28"/></span>',
        '[抠鼻]': '<span><img src="../res/ChatBox/emotion/Expression_42.png"  width="28" /></span>',
        '[鼓掌]': '<span><img src="../res/ChatBox/emotion/Expression_43.png"  width="28" /></span>',
        '[糗大了]': '<span><img src="../res/ChatBox/emotion/Expression_44.png"  width="28" /></span>',
        '[坏笑]': '<span><img src="../res/ChatBox/emotion/Expression_45.png"  width="28" /></span>',
        '[左哼哼]': '<span><img src="../res/ChatBox/emotion/Expression_46.png"  width="28" /></span>',
        '[右哼哼]': '<span><img src="../res/ChatBox/emotion/Expression_47.png"  width="28" /></span>',
        '[哈欠]': '<span><img src="../res/ChatBox/emotion/Expression_48.png"  width="28" /></span>',
        '[鄙视]': '<span><img src="../res/ChatBox/emotion/Expression_49.png"  width="28" /></span>',
        '[委屈]': '<span><img src="../res/ChatBox/emotion/Expression_50.png"  width="28" /></span>',
        '[快哭了]': '<span><img src="../res/ChatBox/emotion/Expression_51.png"  width="28" /></span>',
        '[阴险]': '<span><img src="../res/ChatBox/emotion/Expression_52.png"  width="28" /></span>',
        '[亲亲]': '<span><img src="../res/ChatBox/emotion/Expression_53.png"  width="28" /></span>',
        '[吓]': '<span><img src="../res/ChatBox/emotion/Expression_54.png"  width="28" /></span>',
        '[可怜]': '<span><img src="../res/ChatBox/emotion/Expression_55.png"  width="28" /></span>',
        '[菜刀]': '<span><img src="../res/ChatBox/emotion/Expression_56.png"  width="28" /></span>',
        '[西瓜]': '<span><img src="../res/ChatBox/emotion/Expression_57.png"  width="28" /></span>',
        '[啤酒]': '<span><img src="../res/ChatBox/emotion/Expression_58.png"  width="28" /></span>',
        '[篮球]': '<span><img src="../res/ChatBox/emotion/Expression_59.png"  width="28" /></span>',
        '[乒乓]': '<span><img src="../res/ChatBox/emotion/Expression_60.png"  width="28" /></span>',
        '[咖啡]': '<span><img src="../res/ChatBox/emotion/Expression_61.png"  width="28" /></span>',
        '[饭]': '<span><img src="../res/ChatBox/emotion/Expression_62.png"  width="28" /></span>',
        '[猪头]': '<span><img src="../res/ChatBox/emotion/Expression_63.png"  width="28" /></span>',
        '[玫瑰]': '<span><img src="../res/ChatBox/emotion/Expression_64.png"  width="28" /></span>',
        '[凋谢]': '<span><img src="../res/ChatBox/emotion/Expression_65.png"  width="28" /></span>',
        '[嘴唇]': '<span><img src="../res/ChatBox/emotion/Expression_66.png"  width="28" /></span>',
        '[爱心]': '<span><img src="../res/ChatBox/emotion/Expression_67.png"  width="28" /></span>',
        '[心碎]': '<span><img src="../res/ChatBox/emotion/Expression_68.png"  width="28"/></span>',
        '[蛋糕]': '<span><img src="../res/ChatBox/emotion/Expression_69.png"  width="28"/></span>',
        '[闪电]': '<span><img src="../res/ChatBox/emotion/Expression_70.png"  width="28"/></span>',
        '[炸弹]': '<span><img src="../res/ChatBox/emotion/Expression_71.png"  width="28" /></span>',
        '[刀]': '<span><img src="../res/ChatBox/emotion/Expression_72.png"  width="28" /></span>',
        '[足球]': '<span><img src="../res/ChatBox/emotion/Expression_73.png"  width="28" /></span>',
        '[瓢虫]': '<span><img src="../res/ChatBox/emotion/Expression_74.png"  width="28" /></span>',
        '[便便]': '<span><img src="../res/ChatBox/emotion/Expression_75.png"  width="28" /></span>',
        '[月亮]': '<span><img src="../res/ChatBox/emotion/Expression_76.png"  width="28" /></span>',
        '[太阳]': '<span><img src="../res/ChatBox/emotion/Expression_77.png"  width="28" /></span>',
        '[礼物]': '<span><img src="../res/ChatBox/emotion/Expression_78.png"  width="28" /></span>',
        '[拥抱]': '<span><img src="../res/ChatBox/emotion/Expression_79.png"  width="28" /></span>',
        '[强]': '<span><img src="../res/ChatBox/emotion/Expression_80.png"  width="28" /></span>',
        '[弱]': '<span><img src="../res/ChatBox/emotion/Expression_81.png"  width="28" /></span>',
        '[握手]': '<span><img src="../res/ChatBox/emotion/Expression_82.png"  width="28" /></span>',
        '[胜利]': '<span><img src="../res/ChatBox/emotion/Expression_83.png"  width="28" /></span>',
        '[抱拳]': '<span><img src="../res/ChatBox/emotion/Expression_84.png"  width="28" /></span>',
        '[勾引]': '<span><img src="../res/ChatBox/emotion/Expression_85.png"  width="28" /></span>',
        '[拳头]': '<span><img src="../res/ChatBox/emotion/Expression_86.png"  width="28" /></span>',
        '[差劲]': '<span><img src="../res/ChatBox/emotion/Expression_87.png"  width="28" /></span>',
        '[爱你]': '<span><img src="../res/ChatBox/emotion/Expression_88.png"  width="28" /></span>',
        '[NO]': '<span><img src="../res/ChatBox/emotion/Expression_89.png"  width="28" /></span>',
        '[OK]': '<span><img src="../res/ChatBox/emotion/Expression_90.png"  width="28" /></span>',
        '[爱情]': '<span><img src="../res/ChatBox/emotion/Expression_91.png"  width="28" /></span>',
        '[飞吻]': '<span><img src="../res/ChatBox/emotion/Expression_92.png"  width="28" /></span>',
        '[跳跳]': '<span><img src="../res/ChatBox/emotion/Expression_93.png"  width="28" /></span>',
        '[发抖]': '<span><img src="../res/ChatBox/emotion/Expression_94.png"  width="28" /></span>',
        '[怄火]': '<span><img src="../res/ChatBox/emotion/Expression_95.png"  width="28" /></span>',
        '[转圈]': '<span><img src="../res/ChatBox/emotion/Expression_96.png"  width="28" /></span>',
        '[磕头]': '<span><img src="../res/ChatBox/emotion/Expression_97.png"  width="28"/></span>',
        '[回头]': '<span><img src="../res/ChatBox/emotion/Expression_98.png"  width="28"/></span>',
        '[跳绳]': '<span><img src="../res/ChatBox/emotion/Expression_99.png"  width="28"/></span>',
        '[投降]': '<span><img src="../res/ChatBox/emotion/Expression_100.png"  width="28" /></span>',
        '[激动]': '<span><img src="../res/ChatBox/emotion/Expression_101.png"  width="28" /></span>',
        '[街舞]': '<span><img src="../res/ChatBox/emotion/Expression_102.png"  width="28" /></span>',
        '[献吻]': '<span><img src="../res/ChatBox/emotion/Expression_103.png"  width="28" /></span>',
        '[左太极]': '<span><img src="../res/ChatBox/emotion/Expression_104.png"  width="28" /></span>',
        '[右太极]': '<span><img src="../res/ChatBox/emotion/Expression_105.png"  width="28" /></span>'
    };

    //打开图像
    function openImage(path) {
        var imageBrowser = api.require('imageBrowser');
        imageBrowser.openImages({
            imageUrls: [path]
        });
    }

    //时间差计算
    function sj(sj) {
        var nowt = new Date().getTime();
        var a = new Date(parseInt(sj));
        var b = new Date(parseInt(nowt));
        var date1 = Date.parse(format(a, 4));
        var date2 = Date.parse(format(b, 4));
        var xxsj = Math.ceil((date2 - date1) / (60 * 1000))
        if (xxsj <= 1 && xxsj >= 0) {
            return "就刚才";
        } else if (xxsj <= 10 && xxsj > 1) {
            return xxsj + "分钟前";
        } else if (xxsj <= 60 && xxsj > 10) {
            return format(a, 1);
        } else if (xxsj <= 1440 && xxsj > 60) {
            return format(a, 1);
        } else if (xxsj <= 10080 && xxsj > 1440) {
            return format(a, 2);
        } else if (xxsj > 10080) {
            return format(a, 3);
        } else {
            return format(a, 3);
        }
    }

    //json格式转换
    function transExtra(arg) {
        var result = '';
        try {
            result = eval('(' + arg + ')');
        } catch (e) {
            result = arg.slice(1, -1);
        } finally {
        }
        return result;
    }

    //格式化时间
    function format(now, type) {
        var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
        var year = now.getFullYear().toString();
        var month = (now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        var tian = now.getDay().toString();
        var hour = (now.getHours()).toString();
        var minute = (now.getMinutes()).toString();
        var second = (now.getSeconds()).toString();
        if (hour.length == 1) {
            hour = "0" + hour;
        }
        if (minute.length == 1) {
            minute = "0" + minute;
        }
        if (second.length == 1) {
            second = "0" + second;
        }
        if (type == 1) {
            var dateTime = hour + ":" + minute;
        } else if (type == 2) {
            var dateTime = show_day[tian] + " " + hour + ":" + minute
        } else if (type == 3) {
            var dateTime = year + "-" + month + "-" + day
        } else if (type == 4) {
            var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
        } else if (type == 5) {
            var dateTime = show_day[tian];
        }
        return dateTime;
    }
</script>
</html>
